<?php
/**
 * (logic) 补星级数据
 */
namespace App\Logic\Operation;

use App\Models\Basics\SysCountry;
use App\Models\Operation\BATotal;
use App\Models\Operation\Crawl\ReviewsFile;
use App\Util\RExcel;
use Illuminate\Support\Facades\DB;
class CheckStarOperation
{
    public static function handle($date)
    {
        $country = array_keys(CrawlReviews::$_baseUrl);

        foreach ($country as $_country){
            $countryId = SysCountry::getIdByCountry($_country);
            if(!$countryId)continue;
            $file = ReviewsFile::getFile($date,$countryId);
            $lines = RExcel::importCsvFile($file);
            DB::beginTransaction();
            foreach($lines as $k => $v){
                if($k == 0)continue;
                if(!is_array($v))continue;
                $count = count($v);
                if($count < 6)continue;
                if($v[3] > 5)continue;
                if(strlen($v[4]) > 60)continue;
                if(strlen($v[5]) > 60)continue;

                //运营总表
                $total = BATotal::query()
                    ->where('report_date',$date)
                    ->where('country_id',$countryId)
                    ->where('classify_id',$v[0])
                    ->first();
                if(!$total)continue;
                if($count == 6){
                    $total->star = $v[3];
                    $total->b_rank = $v[4];
                    $total->s_rank = $v[5];
                }else{
                    $total->star = $v[3];
                    $total->ratings = $v[4] ? trim($v[4]) : null;
                    $total->b_rank = $v[5];
                    $total->s_rank = $v[6];
                }
                $total->save();
            }
            DB::commit();
        }
    }

    public static function handlex($date)
    {
        die('no data!');
        $eof = strtotime($date); //日期转时间戳
        $country = array_keys(CrawlReviews::$_baseUrl);
        foreach ($country as $_country){
            $countryId = SysCountry::getIdByCountry($_country);
            if(!$countryId)continue;
            echo '==========start:'.$_country.'========'.PHP_EOL;
            $time = strtotime('2024-01-23');
            while (true){
                if($time == $eof)break;
                $date = date("Y-m-d",$time);
                $file = ReviewsFile::getFile($date,$countryId);
                if(!is_file($file)){
                    $time += 86400;
                    continue;
                }
                $lines = RExcel::importCsvFile($file);
                echo '==========start:'.$_country.':'.$date.'========'.PHP_EOL;
                DB::beginTransaction();
                foreach($lines as $k => $v){
                    if($k == 0)continue;
                    if(!is_array($v))continue;
                    $count = count($v);
                    if($count < 6)continue;
                    if($v[3] > 5)continue;
                    if(strlen($v[4]) > 60)continue;
                    if(strlen($v[5]) > 60)continue;

                    //运营总表
                    $total = BATotal::query()
                        ->where('report_date',$date)
                        ->where('country_id',$countryId)
                        ->where('classify_id',$v[0])
                        ->first();
                    if(!$total)continue;
                    if($count == 6){
                        $total->star = $v[3];
                        $total->b_rank = $v[4];
                        $total->s_rank = $v[5];
                    }else{
                        $total->star = $v[3];
                        $total->ratings = $v[4] ? trim($v[4]) : null;
                        $total->b_rank = $v[5];
                        $total->s_rank = $v[6];
                    }
                    $total->save();
                }
                echo '==========end:'.$_country.':'.$date.'========'.PHP_EOL;
                $time += 86400;
                DB::commit();
            }
            echo '==========end:'.$_country.'========'.PHP_EOL;
        }
    }
}
